package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lst.api.entity.ApplyInfo;
import com.lst.api.mapper.ApplyInfoMapper;
import com.lst.api.service.IApplyInfoService;
import com.lst.api.vo.ApplyInfoVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 借款数据信息表业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Service
public class ApplyInfoService extends BaseService<ApplyInfoMapper, ApplyInfo> implements IApplyInfoService {

    @Override
    public boolean saveOrUpdate(long userId, String applyNo, ApplyInfoVo applyInfoVo) {
        if (applyInfoVo == null) {
            return false;
        }
        applyInfoVo.check();
        ApplyInfo applyInfo = new ApplyInfo(userId, applyNo, applyInfoVo);
        QueryWrapper<ApplyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(ApplyInfo.APPLY_NO, applyNo)
                .eq(ApplyInfo.PROMO_USER_ID, userId);
        int ret = super.saveNotExist(applyInfo, queryWrapper);
        if (ret == 0) {
            UpdateWrapper<ApplyInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .eq(ApplyInfo.APPLY_NO, applyNo)
                    .eq(ApplyInfo.PROMO_USER_ID, userId)
                    .set(ApplyInfo.UPDATE_AT, new Date())
                    .set(ApplyInfo.APPLY_TYPE, applyInfo.getApplyType())
                    .set(ApplyInfo.LOAN_PURPOSE_ID, applyInfo.getLoanPurposeId())
                    .set(ApplyInfo.LOAN_PURPOSE_OTHER, applyInfo.getLoanPurposeOther());
            return super.update(updateWrapper);
        }
        return true;
    }

    @Override
    public ApplyInfo getByApplyNo(String applyNo) {
        QueryWrapper<ApplyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ApplyInfo.APPLY_NO, applyNo);
        return super.getOne(queryWrapper);
    }
}